<template>
  <div>
    <el-divider content-position="left">客户流失预警设置</el-divider>
    <FormPanel ref="form" :list="formList" labelWidth="200" :cols="3" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div class="BTF">
      <AjaxButton :click="saveHandler" type="primary">保存</AjaxButton>
    </div>
  </div>
</template>

<script>
import { saveWarningInfo, getWarningList } from '@/api/customerRepair/customerLoss/lossWarningSet';
export default {
  name: 'LossW',
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      datalist: {}
    };
  },
  mounted() {
    this.getWarningLisst();
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '最小限制',
          fieldName: 'minLimit',
          // precision: 0,
          style: { width: '100px' }
          // change: val => { this.changeType(3,val)}
        },
        {
          type: 'INPUT',
          label: '摇摆限制',
          fieldName: 'lossLimit',
          // precision: 0,
          style: { width: '100px' }
          // change: val => { this.changeType(1,val)}
        },
        {
          type: 'INPUT',
          label: '最大限制',
          fieldName: 'maxLimit',
          // precision: 0,
          style: { width: '100px' }
          // change: val => { this.changeType(2,val)}
        },
        {
          type: 'INPUT_NUMBER',
          label: '预警ID',
          fieldName: 'warningId',
          hidden: true
        }
      ];
    },
    // 表单提交
    formChangeHandle(val) {
      this.saveData = val;
    },
    async getWarningLisst() {
      const res = await getWarningList();
      this.datalist = res.data.rows[0];
      this.formList.find(item => item.fieldName === 'lossLimit').initialValue = this.datalist.lossLimit;
      this.formList.find(item => item.fieldName === 'maxLimit').initialValue = this.datalist.maxLimit;
      this.formList.find(item => item.fieldName === 'minLimit').initialValue = this.datalist.minLimit;
      this.formList.find(item => item.fieldName === 'warningId').initialValue = this.datalist.warningId;
    },
    // 保存表单
    async saveHandler() {

      this.saveData = null;

      this.$refs.form.SUBMIT_FORM();
      let warningId=this.formList.find(item => item.fieldName === 'warningId').initialValue;
      let minLimit = this.formList.find(item => item.fieldName === 'minLimit').initialValue;
      let warningLimit = this.formList.find(item => item.fieldName === 'lossLimit').initialValue;
      let maxLimit = this.formList.find(item => item.fieldName === 'maxLimit').initialValue;
      window.console.log('最小天数================>',minLimit,'摇摆天数================>',warningLimit,'最大天数================>',maxLimit)
      if(minLimit*1>=warningLimit*1){
        this.$notify.warning({ title: '提示信息', message: '最小限制天数不能大于等摇摆限制天数' });
        return false;
      }
      if(warningLimit*1>=maxLimit*1){
        this.$notify.warning({ title: '提示信息', message: '摇摆限制天数不能大于等最大限制天数' });
        return false;
      }
      if(minLimit*1>=maxLimit*1){
        this.$notify.warning({ title: '提示信息', message: '最小限制天数不能大于等最大限制天数' });
        return false;
      }




      let obj = Object.assign({warningId:warningId}, this.saveData);
      let res = await saveWarningInfo(obj);
      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '保存成功' });
        this.getWarningLisst();
      } else {
        return this.$notify.warning({ title: '提示信息', message: '有校验信息未通过' });
      }
    },
    changeType(flag,val){
      if(flag ==1 ){
        let maxLimit = this.formList.find(item => item.fieldName === 'maxLimit').initialValue ;// 最大
        let minLimit = this.formList.find(item => item.fieldName === 'minLimit').initialValue ;// 最小
        if(val - maxLimit >= 0 ){
            this.formList.find(item => item.fieldName === 'lossLimit').initialValue ='';
            this.$notify.warning({ title: '提示信息', message: '摇摆限制不能大于最大限制' });
            return false;
        }
        if(minLimit!= ''){
          if(val - minLimit <= 0 ){
            this.formList.find(item => item.fieldName === 'lossLimit').initialValue ='';
            this.$notify.warning({ title: '提示信息', message: '摇摆限制不能小于最小限制' });
            return false;
          }
        }

      }else if(flag ==2){
        let lossLimit = this.formList.find(item => item.fieldName === 'lossLimit').initialValue ;// 摇摆
        let minLimit = this.formList.find(item => item.fieldName === 'minLimit').initialValue ;// 最小
        if(lossLimit!=''){
          if(lossLimit - val >= 0 ){
            this.formList.find(item => item.fieldName === 'maxLimit').initialValue ='';
            this.$notify.warning({ title: '提示信息', message: '最大限制不能小于摇摆限制' });
            return false;
          }
        }
        if(minLimit!= ''){
          if(val - minLimit <= 0 ){
            this.formList.find(item => item.fieldName === 'maxLimit').initialValue ='';
            this.$notify.warning({ title: '提示信息', message: '最大限制不能小于最小限制' });
            return false;
          }
        }
      }else if(flag ==3){
        let lossLimit = this.formList.find(item => item.fieldName === 'lossLimit').initialValue ;// 摇摆
        let maxLimit = this.formList.find(item => item.fieldName === 'maxLimit').initialValue ;// 最大
        if(lossLimit!=''){
          if(val - lossLimit  >= 0 ){
            this.formList.find(item => item.fieldName === 'minLimit').initialValue ='';
            this.$notify.warning({ title: '提示信息', message: '最小限制不能大于摇摆限制' });
            return false;
          }
        }
        if(maxLimit!= ''){
          if(val - maxLimit >= 0 ){
            this.formList.find(item => item.fieldName === 'minLimit').initialValue ='';
            this.$notify.warning({ title: '提示信息', message: '最小限制不能大于最大限制' });
            return false;
          }
        }
      }
    }
  }
};
</script>

<style scoped>
.BTF {
  text-align: center;
}
</style>
